home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr57 / mentc.zip / SCREEN.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  10KB  |  411 lines

  1. /*    SCREEN.C:    Screen manipulation commands
  2.             for MicroEMACS 3.11
  3.             written by Daniel Lawrence
  4. */
  5.  
  6. #include    <stdio.h>
  7. #include    "estruct.h"
  8. #include    "eproto.h"
  9. #include        "edef.h"
  10. #include    "elang.h"
  11.  
  12. #if    0
  13. dumpscreens(msg)
  14.  
  15. char *msg;
  16.  
  17. {
  18.     SCREEN *sp;
  19.  
  20.     printf("<%s>\n", msg);
  21.     sp = first_screen;
  22.     while (sp != (SCREEN *)NULL) {
  23.         printf("%lu - [%s] %d/%d to %d/%d \n", sp, sp->s_screen_name,
  24.         sp->s_roworg, sp->s_colorg, sp->s_nrow, sp->s_ncol);
  25.         sp = sp->s_next_screen;
  26.     }
  27.     printf("   0     -   [EOL]\n");
  28.     tgetc();
  29. }
  30. #endif
  31.  
  32. /*    This command takes the last window in the linked window list,
  33.     which is visibly rearmost, and brings it to front. It is bound
  34.     to A-N on machines with an ALT key
  35. */
  36.  
  37. PASCAL NEAR cycle_screens(f, n)
  38.  
  39. int f,n;    /* prefix flag and argument */
  40.  
  41. {
  42.     SCREEN *sp;        /* ptr to screen to switch to */
  43.  
  44.     /* find the last screen */
  45.     sp = first_screen;
  46.     while (sp->s_next_screen != (SCREEN *)NULL)
  47.         sp = sp->s_next_screen;
  48.  
  49.     /* and make this screen current */
  50.     return(select_screen(sp, TRUE));
  51. }
  52.  
  53. PASCAL NEAR find_screen(f, n)
  54.  
  55. int f,n;    /* prefix flag and argument */
  56.  
  57. {
  58.     char scr_name[NSTRING];    /* buffer to hold screen name */
  59.     SCREEN *sp;        /* ptr to screen to switch to */
  60.  
  61.     /* get the name of the screen to switch to */
  62.     mlreply("Find Screen: ", scr_name, NSTRING);
  63.     sp = lookup_screen(scr_name);
  64.  
  65.     if (sp == (SCREEN *)NULL) {
  66.  
  67.         /* save the current dot position in the buffer info
  68.            so the new screen will start there! */
  69.         curbp->b_dotp = curwp->w_dotp;
  70.         curbp->b_doto = curwp->w_doto;
  71.  
  72.         /* screen does not exist, create it */
  73.         sp = init_screen(scr_name, curbp);
  74.     }
  75.  
  76.     /* and make this screen current */
  77.     return(select_screen(sp, TRUE));
  78. }
  79.  
  80. PASCAL NEAR free_screen(sp)    /* free all resouces associated with a screen */
  81.  
  82. SCREEN *sp;    /* screen to dump */
  83.  
  84. {
  85.     register int cmark;    /* mark ordinal index */
  86.     register WINDOW *wp;    /* ptr to window to free */
  87.     register WINDOW *tp;    /* temp window pointer */
  88.  
  89.     /* first, free the screen's windows */
  90.     wp = sp->s_first_window;
  91.     while (wp) {
  92.         if (--wp->w_bufp->b_nwnd == 0) {
  93.             wp->w_bufp->b_dotp  = wp->w_dotp;
  94.             wp->w_bufp->b_doto  = wp->w_doto;
  95.             for (cmark = 0; cmark < NMARKS; cmark++) {
  96.                 wp->w_bufp->b_markp[cmark] = wp->w_markp[cmark];
  97.                 wp->w_bufp->b_marko[cmark] = wp->w_marko[cmark];
  98.             }
  99.             wp->w_bufp->b_fcol  = wp->w_fcol;
  100.         }
  101.  
  102.         /* on to the next window, free this one */
  103.         tp = wp->w_wndp;
  104.         free((char *) wp);
  105.         wp = tp;
  106.     }
  107.  
  108.     /* and now, free the screen struct itself */
  109.     free(sp->s_screen_name);
  110.     free((char *) sp);
  111.  
  112.     return (0);
  113. }
  114.  
  115. PASCAL NEAR delete_screen(f, n)
  116.  
  117. int f,n;    /* prefix flag and argument */
  118.  
  119. {
  120.     char scr_name[NSTRING];    /* buffer to hold screen name */
  121.     SCREEN *sp;        /* ptr to screen to switch to */
  122.     SCREEN *last_scr;    /* screen previous to one to delete */
  123.  
  124.     /* get the name of the screen to delete */
  125.     mlreply("Delete Screen: ", scr_name, NSTRING);
  126.     sp = lookup_screen(scr_name);
  127.  
  128.     /* make sure it exists... */
  129.     if (sp == (SCREEN *)NULL) {
  130.         mlwrite("[No such screen]");
  131.         return(FALSE);
  132.     }
  133.  
  134.     /* it can't be current... */
  135.     if (sp == first_screen) {
  136.         mlwrite("%%Can't delete current screen");
  137.         return(FALSE);
  138.     }
  139.  
  140.     /* find the place to delete */
  141.     last_scr = first_screen;
  142.     while (last_scr) {
  143.         if (last_scr->s_next_screen == sp)
  144.             break;
  145.         last_scr = last_scr->s_next_screen;
  146.     }
  147.     last_scr->s_next_screen = sp->s_next_screen;
  148.     free_screen(sp);
  149. #if    WINDOW_TEXT
  150.             refresh_screen(first_screen);
  151. #endif
  152.     return(TRUE);
  153. }
  154.  
  155. /* this function initializes a new screen.... */
  156.  
  157. SCREEN *PASCAL NEAR init_screen(scr_name, scr_buf)
  158.  
  159. char *scr_name;        /* screen name */
  160. BUFFER *scr_buf;    /* buffer to place in first window of screen */
  161.  
  162. {
  163.     int cmark;        /* current mark to initialize */
  164.     SCREEN *sp;        /* pointer to allocated screen */
  165.     SCREEN *last_sp;    /* pointer to last screen */
  166.     WINDOW *wp;        /* ptr to first window of new screen */
  167.  
  168.     /* allocate memory for this screen */
  169.     sp = (SCREEN *)malloc(sizeof(SCREEN));
  170.     if (sp == (SCREEN *)NULL)
  171.         return(sp);
  172.  
  173.     /* set up this new screens fields! */
  174.     sp->s_next_screen = (SCREEN *)NULL;
  175.     sp->s_screen_name = copystr(scr_name);
  176.     sp->s_roworg = term.t_roworg;
  177.     sp->s_colorg = term.t_colorg;
  178.     sp->s_nrow = term.t_nrow;
  179.     sp->s_ncol = term.t_ncol;
  180.  
  181.     /* allocate its first window */
  182.     wp = (WINDOW *)malloc(sizeof(WINDOW));
  183.     if (wp == (WINDOW *)NULL) {
  184.         free((char *)sp);
  185.         return((SCREEN *)NULL);
  186.     }
  187.     sp->s_first_window = sp->s_cur_window = wp;
  188.  
  189.     /* and setup the windows info */
  190.     wp->w_wndp = NULL;
  191.     wp->w_bufp = scr_buf;
  192.     scr_buf->b_nwnd += 1;    
  193.     wp->w_linep = scr_buf->b_linep;
  194.  
  195.     /* position us at the buffers dot */
  196.     wp->w_dotp  = scr_buf->b_dotp;
  197.     wp->w_doto  = scr_buf->b_doto;
  198.  
  199.     /* set all the marks to UNSET */
  200.     for (cmark = 0; cmark < NMARKS; cmark++) {
  201.         wp->w_markp[cmark] = NULL;
  202.         wp->w_marko[cmark] = 0;
  203.     }
  204.     wp->w_toprow = 0;
  205. #if    COLOR
  206.     /* initalize colors to global defaults */
  207.     wp->w_fcolor = gfcolor;
  208.     wp->w_bcolor = gbcolor;
  209. #endif
  210.     wp->w_fcol = 0;
  211.     wp->w_ntrows = term.t_nrow-1;        /* "-1" for mode line.    */
  212.     wp->w_force = 0;
  213.     wp->w_flag  = WFMODE|WFHARD;        /* Full.        */
  214.  
  215.     /* first screen? */
  216.     if (first_screen == (SCREEN *)NULL) {
  217.         first_screen = sp;
  218.         return(sp);
  219.     }
  220.  
  221.     /* insert it at the tail of the screen list */
  222.     last_sp = first_screen;
  223.     while (last_sp->s_next_screen != (SCREEN *)NULL)
  224.         last_sp = last_sp->s_next_screen;
  225.     last_sp->s_next_screen = sp;
  226.  
  227.     /* and return the new screen pointer */
  228.     return(sp);
  229. }
  230.  
  231. SCREEN *PASCAL NEAR lookup_screen(scr_name)
  232.  
  233. char *scr_name;        /* named screen to find */
  234.  
  235. {
  236.     SCREEN *result;
  237.  
  238.     /* scan the screen list */
  239.     result = first_screen;
  240.     while (result) {
  241.  
  242.         /* if this is it, return its handle! */
  243.         if (strcmp(scr_name, result->s_screen_name) == 0)
  244.             return(result);
  245.  
  246.         /* on to the next screen */
  247.         result = result->s_next_screen;
  248.     }
  249.  
  250.     /* we didn't find it..... */
  251.     return((SCREEN *)NULL);
  252. }
  253.  
  254. int PASCAL NEAR select_screen(sp, announce)
  255.  
  256. SCREEN *sp;    /* ptr to screen to switch to */
  257. int announce;    /* announce the selection? */
  258.  
  259. {
  260.     WINDOW *temp_wp;    /* backup of current window ptr (curwp) */
  261.     SCREEN *temp_screen;    /* temp ptr into screen list */
  262.  
  263.     /* make sure there is something here to set to! */
  264.     if (sp == (SCREEN *)NULL)
  265.         return(FALSE);
  266.  
  267.     /* nothing to do, it is already current */
  268.     if (sp == first_screen)
  269.         return(TRUE);
  270.  
  271.     /* deselect the current window */
  272.     temp_wp = curwp;
  273.     curwp = (WINDOW *)NULL;
  274.     modeline(temp_wp);
  275.     updupd(TRUE);
  276.     curwp = temp_wp;
  277.  
  278.     /* save the current screens concept of current window */
  279.     first_screen->s_cur_window = curwp;
  280.     first_screen->s_roworg = term.t_roworg;
  281.     first_screen->s_colorg = term.t_colorg;
  282.     first_screen->s_nrow = term.t_nrow;
  283.     first_screen->s_ncol = term.t_ncol;
  284.  
  285.     /* re-order the screen list */
  286.     temp_screen = first_screen;
  287.     while (temp_screen->s_next_screen != (SCREEN *)NULL) {
  288.         if (temp_screen->s_next_screen == sp) {
  289.             temp_screen->s_next_screen = sp->s_next_screen;
  290.             break;
  291.         }
  292.         temp_screen = temp_screen->s_next_screen;
  293.     }
  294.     sp->s_next_screen = first_screen;
  295.     first_screen = sp;
  296.  
  297.     /* reset the current screen, window and buffer */
  298.     wheadp = first_screen->s_first_window;
  299.     curwp = first_screen->s_cur_window;
  300.     curbp = curwp->w_bufp;
  301.  
  302.     /* let the display driver know we need a full screen update */
  303.     update_size();
  304.     upwind();
  305.     if (announce) {
  306.         mlwrite(TEXT225, first_screen->s_screen_name);
  307. /*            "[Switched to screen %s]" */
  308.     }
  309.     return(TRUE);
  310. }
  311.  
  312. /*    Build and popup a buffer containing the list of all screens.
  313.     Bound to "A-B".
  314. */
  315.  
  316. PASCAL NEAR list_screens(f, n)
  317.  
  318. int f,n;    /* prefix flag and argument */
  319.  
  320. {
  321.     register int status;    /* stutus return */
  322.  
  323.     if ((status = screenlist(f)) != TRUE)
  324.         return(status);
  325.     return(wpopup(slistp));
  326. }
  327.  
  328.  
  329. /*
  330.  * This routine rebuilds the
  331.  * text in the special secret buffer
  332.  * that holds the screen list. It is called
  333.  * by the list screens command. Return TRUE
  334.  * if everything works. Return FALSE if there
  335.  * is an error (if there is no memory). Iflag
  336.  * indecates weather to list hidden screens.
  337.  */
  338. PASCAL NEAR screenlist(iflag)
  339.  
  340. int iflag;    /* list hidden screen flag */
  341.  
  342. {
  343.     SCREEN *sp;        /* ptr to current screen to list */
  344.     WINDOW *wp;        /* ptr into current screens window list */
  345.     int status;        /* return status from functions */
  346.     char line[NSTRING];    /* buffer to construct list lines */
  347.     char bname[NSTRING];    /* name of next buffer */
  348.  
  349.     /* mark this buffer as unchanged so... */
  350.     slistp->b_flag &= ~BFCHG;
  351.  
  352.     /* we can dump it's old contents without complaint */
  353.     if ((status = bclear(slistp)) != TRUE)
  354.         return(status);
  355.  
  356.     /* there is no file connected with this buffer */
  357.     strcpy(slistp->b_fname, "");
  358.  
  359.     /* construct the header of this list */
  360.     if (addline(slistp, "Screen         Buffers") == FALSE
  361.      || addline(slistp, "------         -------") == FALSE)
  362.         return(FALSE);
  363.  
  364.     /* starting from the first screen */
  365.     sp = first_screen;
  366.  
  367.     /* scan all the screens */
  368.     while (sp) {
  369.  
  370.         /* construct the screen name */
  371.         strcpy(line, sp->s_screen_name);
  372.         strcat(line, "                ");
  373.         line[15] = 0;
  374.  
  375.         /* list this screens windows's buffer names */
  376.         wp = sp->s_first_window;
  377.         while (wp) {
  378.  
  379.             /* grab this window's buffer name */
  380.             strcpy(bname, wp->w_bufp->b_bname);
  381.  
  382.             /* handle full lines */
  383.             if (strlen(line) + strlen(bname) + 1 > 78) {
  384.                 if (addline(slistp, line) == FALSE)
  385.                     return(FALSE);
  386.                 strcpy(line, "               ");
  387.             }
  388.  
  389.             /* append this buffer name */
  390.             if (strlen(line) > 15)
  391.                 strcat(line, " ");
  392.             strcat(line, bname);
  393.  
  394.             /* on to the next window */
  395.             wp = wp->w_wndp;
  396.         }
  397.  
  398.         /* and add the line to the buffer */
  399.         if (addline(slistp, line) == FALSE)
  400.             return(FALSE);
  401.  
  402.         /* on to the next screen */
  403.         sp = sp->s_next_screen;
  404.     }
  405.  
  406.     /* all constructed! */
  407.     return(TRUE);
  408. }
  409.  
  410.  
  411.